package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qaml.mes.device.dao.DayOffConfigDO;
import com.qaml.mes.device.dao.mapper.DayOffConfigMapper;
import com.qaml.mes.device.repository.DayOffRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.lang.invoke.LambdaMetafactory;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Repository
public class DayOffRepositoryImpl implements DayOffRepository {


    @Autowired
    private DayOffConfigMapper dayOffConfigMapper;

    @Override
    public void save(DayOffConfigDO configDO, Boolean isUpdate) {
        if (isUpdate) {
            dayOffConfigMapper.updateById(configDO);
        } else {
            dayOffConfigMapper.insert(configDO);
        }
    }

    @Override
    public IPage<DayOffConfigDO> page(Integer page, Integer pageSize, DayOffConfigDO configDO) {
        Page<DayOffConfigDO> objectPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<DayOffConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DayOffConfigDO::getIsDeleted, 0);
        queryWrapper.eq(DayOffConfigDO::getTenantId, configDO.getTenantId());
        queryWrapper.orderByDesc(DayOffConfigDO::getStartTime);
        return dayOffConfigMapper.selectPage(objectPage, queryWrapper);
    }

    @Override
    public List<DayOffConfigDO> getDoByTenantAndDate(Long tenantId, Date date) {
        LambdaQueryWrapper<DayOffConfigDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DayOffConfigDO::getIsDeleted, 0);
        queryWrapper.eq(DayOffConfigDO::getTenantId, tenantId);
        queryWrapper.ge(DayOffConfigDO::getEndTime, date);
        queryWrapper.le(DayOffConfigDO::getStartTime, date);
        return dayOffConfigMapper.selectList(queryWrapper);
    }
}
